from sqlalchemy import Column, String, DateTime, ForeignKey, Text, Enum, Integer, Boolean
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.database.base import Base
import uuid
import enum

class MaintenanceType(str, enum.Enum):
    """维护类型枚举"""
    INSPECTION = "inspection"  # 点检
    MAINTENANCE = "maintenance"  # 保养
    REPAIR = "repair"  # 维修
    CALIBRATION = "calibration"  # 校准

class MaintenanceStatus(str, enum.Enum):
    """维护状态枚举"""
    SCHEDULED = "scheduled"  # 已安排
    IN_PROGRESS = "in_progress"  # 进行中
    COMPLETED = "completed"  # 已完成
    CANCELLED = "cancelled"  # 已取消
    OVERDUE = "overdue"  # 逾期

class MaintenanceRecord(Base):
    """设备维护记录模型"""
    __tablename__ = "maintenance_records"
    
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    device_id = Column(UUID(as_uuid=True), ForeignKey("devices.id"), nullable=False, index=True)
    maintenance_type = Column(Enum(MaintenanceType), nullable=False, index=True)
    status = Column(Enum(MaintenanceStatus), default=MaintenanceStatus.SCHEDULED, index=True)
    
    # 基本信息
    title = Column(String(255), nullable=False)
    description = Column(Text)
    priority = Column(String(20), default="medium")  # low, medium, high, urgent
    
    # 时间信息
    scheduled_at = Column(DateTime(timezone=True), nullable=False, index=True)
    started_at = Column(DateTime(timezone=True))
    completed_at = Column(DateTime(timezone=True))
    estimated_duration = Column(Integer)  # 预计持续时间(分钟)
    actual_duration = Column(Integer)  # 实际持续时间(分钟)
    
    # 人员信息
    assigned_to = Column(UUID(as_uuid=True), ForeignKey("users.id"))  # 分配给谁
    performed_by = Column(UUID(as_uuid=True), ForeignKey("users.id"))  # 实际执行者
    
    # 维护结果
    result = Column(Text)  # 维护结果描述
    issues_found = Column(Text)  # 发现的问题
    actions_taken = Column(Text)  # 采取的措施
    parts_replaced = Column(Text)  # 更换的零件
    cost = Column(Integer)  # 成本(分为单位)
    
    # 质量评估
    quality_rating = Column(Integer)  # 质量评分(1-5)
    is_successful = Column(Boolean, default=True)  # 是否成功完成
    requires_followup = Column(Boolean, default=False)  # 是否需要后续跟进
    followup_date = Column(DateTime(timezone=True))  # 后续跟进日期
    
    # 审核信息
    approved_by = Column(UUID(as_uuid=True), ForeignKey("users.id"))  # 审核人
    approved_at = Column(DateTime(timezone=True))  # 审核时间
    
    # 系统字段
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    created_by = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
    
    # 关系
    device = relationship("Device", back_populates="maintenance_records")
    assigned_user = relationship("User", foreign_keys=[assigned_to], back_populates="assigned_maintenances")
    performed_user = relationship("User", foreign_keys=[performed_by], back_populates="performed_maintenances")
    approved_user = relationship("User", foreign_keys=[approved_by], back_populates="approved_maintenances")
    creator = relationship("User", foreign_keys=[created_by], back_populates="created_maintenances")